package TankAgent;

import jade.content.onto.basic.Action;
import jade.core.Agent;
import jade.domain.FIPAAgentManagement.FailureException;
import jade.domain.FIPAAgentManagement.NotUnderstoodException;
import jade.domain.FIPAAgentManagement.RefuseException;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.proto.SimpleAchieveREResponder;
import wwtp.ontology.Bypass;
import wwtp.ontology.DirectSendWater;
import wwtp.ontology.Retain;

public class ControlFlowBehaviour extends SimpleAchieveREResponder{

	public ControlFlowBehaviour(Agent arg0, MessageTemplate arg1) {
		super(arg0, arg1);
		// TODO Auto-generated constructor stub
	}
	
	@Override
	protected ACLMessage prepareResponse(ACLMessage request)
			throws NotUnderstoodException, RefuseException {

		try{
			//Do the action Requested by the wwtp

			Action action = (Action)myAgent.getContentManager().extractContent(request);

			if (action.getAction() instanceof DirectSendWater){
				System.out.println("Decision "+ request.getSender().getLocalName()+" : DIRECTSENDWATER");
				((TankAgent)myAgent).directSendWater(((DirectSendWater)action.getAction()).getWater());
			}
			else if (action.getAction() instanceof Retain){
				System.out.println("Decision "+ request.getSender().getLocalName()+" : RETAIN");
				((TankAgent)myAgent).retain(((Retain)action.getAction()).getWater());
			}
			else if (action.getAction() instanceof Bypass){
				System.out.println("Decision "+ request.getSender().getLocalName()+" : BYPASS");
				((TankAgent)myAgent).bypass(((Bypass)action.getAction()).getWater(), ((Bypass)action.getAction()).getReceiver().getName());
			}
		}
		catch (Exception e){
			e.printStackTrace();
		}

		ACLMessage response = request.createReply();
		response.setPerformative(ACLMessage.INFORM);
		return response;

	}

	@Override
	protected ACLMessage prepareResultNotification(ACLMessage request,
			ACLMessage response) throws FailureException {
		// TODO Auto-generated method stub

		response.setPerformative(ACLMessage.INFORM);
		return response;
	}

}
